package net.sudot.chess.business.dao;

import net.sudot.chess.business.model.GameRankLimit;
import org.springframework.stereotype.Repository;

import javax.persistence.TypedQuery;

/**
 * 游戏等级限制(即:某个游戏可以参加的用户等级)
 *
 * @author tangjialin on 2018-02-27.
 */
@Repository
public class GameRankLimitDao extends BaseDao<GameRankLimit> {

    /**
     * 检测是否存在一条数据同时满足传入的参数
     *
     * @param gameId         游戏主键
     * @param customerRankId 客户等级主键
     * @return 返回一个布尔类型 true:满足,false:不满足
     */
    public boolean exist(Long gameId, Long customerRankId) {
        String ql = "SELECT COUNT(gameRankLimit) FROM GameRankLimit gameRankLimit WHERE gameRankLimit.gameId=:gameId AND gameRankLimit.customerRankId=:customerRankId";
        TypedQuery<Number> query = entityManager.createQuery(ql, Number.class);
        query.setParameter("gameId", gameId);
        query.setParameter("customerRankId", customerRankId);
        Number result = query.getSingleResult();
        return result != null && result.longValue() > 0L;
    }
}
